/**
 * @author yxw
 * date : 2013年10月4日 下午10:15:13 
 */
package com.tzinfo.tzt.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.tzinfo.tzt.R;
import com.tzinfo.tzt.activity.util.MyApplication;
import com.tzinfo.tzt.config.FilterConfig;

public class ApartmentSearchActivity extends BaseActivity implements
		OnClickListener {

	private Button gpsBtn;
	private EditText searchEditText;
	private ListView apartmenList;
	private SimpleAdapter simpleAdapter;
	private List<Map<String, Object>> data;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search_apartment);
		init();
	}

	private void init() {
		searchEditText = (EditText) this.findViewById(R.id.et_search);
		gpsBtn = (Button) this.findViewById(R.id.bt_local);
		gpsBtn.setOnClickListener(this);
		apartmenList = (ListView) this
				.findViewById(R.id.listview_result_apartment);
		if (MyApplication.locationCode == -1) {
			String sLocationCode = sharedPreferences
					.readMessage("locationCode");
			if (sLocationCode == null || "".equals(sLocationCode)) {
				Intent intent = new Intent(this, SelectCityActivity.class);
				startActivityForResult(intent, 1);
			} else {
				MyApplication.locationCode = Integer.parseInt(sLocationCode);
				String s = FilterConfig.FILTER_CONTENT[0][MyApplication.locationCode];
				gpsBtn.setText(s);
			}
		} else {
			String s = FilterConfig.FILTER_CONTENT[0][MyApplication.locationCode];
			gpsBtn.setText(s);
		}

		getData();
		simpleAdapter = new SimpleAdapter(this, data,
				R.layout.item_search_result,
				new String[] { "name", "address" }, new int[] { R.id.text_name,
						R.id.text_address });
		apartmenList.setAdapter(simpleAdapter);
		searchEditText.addTextChangedListener(mTextWatcher);
	}

	private void getData() {
		data = new ArrayList<Map<String, Object>>();
		Map<String, Object> map;
		map = new HashMap<String, Object>();
		map.put("name", "浪漫和山");
		map.put("address", "小和山高教园区");
		data.add(map);
		map = new HashMap<String, Object>();
		map.put("name", "小和山");
		map.put("address", "余杭 闲林");
		data.add(map);
		map = new HashMap<String, Object>();
		map.put("name", "下沙");
		map.put("address", "下沙高教园区");
		data.add(map);

	}

	TextWatcher mTextWatcher = new TextWatcher() {
		String text = "";

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			// TODO Auto-generated method stub
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			// TODO Auto-generated method stub
		}

		@Override
		public void afterTextChanged(Editable s) {
			// TODO Auto-generated method stub

			text = searchEditText.getText().toString();
			data.clear();
			if (text == null || "".endsWith(text)) {
				Map<String, Object> map;
				map = new HashMap<String, Object>();
				map.put("name", "搜索：" + "空");
				map.put("address", "搜索：" + "空");
				data.add(map);
				simpleAdapter.notifyDataSetChanged();
			} else {
				Map<String, Object> map;
				map = new HashMap<String, Object>();
				map.put("name", "搜索：" + text);
				map.put("address", "搜索：" + text);
				data.add(map);
				simpleAdapter.notifyDataSetChanged();
			}

		}
	};

	// 不使用监听是因为在布局文件中已经定义
	public void back(View view) {
		this.finish();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_local:
			Intent intent = new Intent(ApartmentSearchActivity.this,
					SelectCityActivity.class);
			startActivity(intent);
			break;

		default:
			break;
		}

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		String s = FilterConfig.FILTER_CONTENT[0][MyApplication.locationCode];
//		if (MyApplication.locationCode != 0) {
//			s = s.substring(0, s.length() - 1);
//		}
		gpsBtn.setText(s);
	}
}
